const app = getApp();
const baseUrl = app.globalData.baseUrl,
	common = require('../../../utils/common'),
	requestinfo = require('../../../utils/requertinfo'),
	// 失物招领类型
	type = 1;
// iconflag 定时器函数
let iconflag;
Page({
	data: {
		StatusBar: app.globalData.StatusBar,
		CustomBar: app.globalData.CustomBar,
		// 是底部发布说说是否显示的标志
		showicon: true,
		showModal: false,
		pageIndex: 1,
		// theme是头部的标志，attention是是否关注
		theme: {
			theme_img: '../../../images/home/lostAndFind/lost_icon.png',
			theme_title: '失物招领',
			theme_intr: '我的主人走丢了，你可以帮我找到他吗？',
			attention: false
		},
		// flag是广场和分类的标志， 为1的时候是广场， 为2的时候是分类。
		flag: 1,
		startPoint: [0, 0],
		reachbottom: false,
		// scrollInfo是分类里面的数据
		scrollInfo: {
			currentIndex: 0,
			indexArray: []
		},
		// comment是广场里的主内容
		comment: [],
		makesureFunc: {
			cancelArr: [{
				text: '取消',
				func : 'hideModal'
			}],
			fucArr :[{
				text : '确定取消关注',
				func : 'hideandcancel'
			}]
		}
	},
	onLoad() {
		// console.log(getCurrentPages());
		let temp;
		wx.getStorage({
			key: 'loginInfo',
			success: function (res) {
				// success
				temp = res.data.Uid;
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		})
		// console.log(111);
		const Uid = app.globalData.loginInfo.Uid || temp;
		let that = this;
		let sectype = this.data.scrollInfo.currentIndex;
		let indexArray = "scrollInfo.indexArray";

		wx.request({
			url: baseUrl + '/api/user/isattention',
			data: {
				Uid: Uid,
				attention: 1
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				let themeattention = "theme.attention";
				if (res.data.code === 200) {
					that.setData({
						[themeattention]: true
					});
				} else if (res.data.code === 201) {
					that.setData({
						[themeattention]: false
					});
				} else {
					console.log("后台错误");
				}

				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		});
		var needdata = {
			this: that,
			baseUrl: baseUrl,
			url: '/api/dynamic/getdynamic',
			pageIndex: that.data.pageIndex,
			type: type,
			Uid: Uid,
			sectype: sectype,
			ifnew: false,
			comment: [],
			callback: function () {
				// console.log("加载完成");
			}
		};
		requestinfo.requestinfo(needdata);
		// console.log(this);
		wx.request({
			url: baseUrl + '/api/dynamic/getdetailindex',
			data: {
				type
			},
			method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				that.setData({
					[indexArray]: res.data.data
				});
				// console.log(res.data.data);
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		})
	},
	onShow() {
		this.setData({
			pageIndex: 1
		});
		this.onLoad();
	},
	switchflag2() {
		let flag = this.data.flag;
		if (flag === 1) {
			this.setData({
				flag: 2
			});
		}
	},
	switchflag1() {
		let flag = this.data.flag;
		let changecurrent = "scrollInfo.currentIndex";
		if (flag === 2) {
			this.setData({
				flag: 1,
				[changecurrent]: 0
			});
		}

		const Uid = app.globalData.loginInfo.Uid;
		let that = this;
		let sectype = this.data.scrollInfo.currentIndex;
		var needdata = {
			this: that,
			baseUrl: baseUrl,
			url: '/api/dynamic/getdynamic',
			pageIndex: that.data.pageIndex,
			type: type,
			Uid: Uid,
			sectype: sectype,
			ifnew: false,
			comment: [],
			callback: function () {
				// console.log("加载完成");

			}
		};
		requestinfo.requestinfo(needdata);
	},
	changeindex(e) {
		let sectype = e.currentTarget.dataset.index;
		let changeIndex = "scrollInfo.currentIndex";
		this.setData({
			[changeIndex]: sectype
		});
		let pageindex = 1;
		const Uid = app.globalData.loginInfo.Uid;
		this.setData({
			pageIndex: pageindex
		});
		let that = this;
		let comment = this.data.comment;
		var needdata = {
			this: that,
			baseUrl: baseUrl,
			url: '/api/dynamic/getdynamic',
			pageIndex: that.data.pageIndex,
			type: type,
			Uid: Uid,
			sectype: sectype,
			ifnew: true,
			comment: comment,
			callback: function () {
				// console.log("加载完成");
			}
		};

		requestinfo.requestinfo(needdata);
	},
	onReachBottom() {
		let pageindex = this.data.pageIndex + 1;
		const Uid = app.globalData.loginInfo.Uid;
		this.setData({
			pageIndex: pageindex
		});
		let that = this;
		let comment = this.data.comment;
		let sectype = this.data.scrollInfo.currentIndex;
		var needdata = {
			this: that,
			baseUrl: baseUrl,
			url: '/api/dynamic/getdynamic',
			pageIndex: that.data.pageIndex,
			type: type,
			Uid: Uid,
			sectype: sectype,
			ifnew: false,
			comment: comment,
			callback: function () {
				// alert("加载完成");
			}
		};
		requestinfo.requestinfo(needdata);
	},
	switchattention() {
		let ifAttention = this.data.theme.attention;
		const Uid = app.globalData.loginInfo.Uid;
		let that = this;
		// 未关注
		if (!ifAttention) {
			wx.request({
				url: baseUrl + '/api/user/setattention',
				data: {
					Uid: Uid,
					attention: 1
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					let themeattention = "theme.attention";
					if (res.data.code === 200) {
						that.setData({
							[themeattention]: true
						});
					}
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		} else {
			this.setData({
				showModal: true
			})
		}
	},
	mytouchstart(e) {
		this.setData({
			startPoint: [e.touches[0].pageX, e.touches[0].pageY]
		});
	},
	mytouchmove(e) {
		var curPoint = [e.touches[0].pageX, e.touches[0].pageY];
		var startPoint = this.data.startPoint;
		if (curPoint[1] >= startPoint[1]) {
			if (this.data.showicon == false) {
				this.setData({
					showicon: true
				});
			}
			//下滑显示
		} else {
			// 上划消失
			if (iconflag) {
				clearTimeout(iconflag);
			}
			if (this.data.showicon == true) {
				this.setData({
					showicon: false
				});
			}
			iconflag = setTimeout(() => {
				this.setData({
					showicon: true
				});
			}, 2000);

		}
	},
	onPullDownRefresh() {
		let pageindex = 1;
		const Uid = app.globalData.loginInfo.Uid;
		this.setData({
			pageIndex: pageindex
		});
		let that = this;
		let sectype = this.data.scrollInfo.currentIndex;
		var needdata = {
			this: that,
			baseUrl: baseUrl,
			url: '/api/dynamic/getdynamic',
			pageIndex: that.data.pageIndex,
			type: type,
			Uid: Uid,
			sectype: sectype,
			ifnew: true,
			comment: [],
			callback: function () {
				wx.stopPullDownRefresh();
			}
		};

		requestinfo.requestinfo(needdata);
	},
	likeAction(e) {
		const Uid = app.globalData.loginInfo.Uid;
		const Did = e.currentTarget.dataset.did;
		const type = 1;
		let needindex = common.searchMem(this.data.comment, 'Did', Did);
		let changeLike = "comment[" + needindex + "].isLike",
			changeNum = "comment[" + needindex + "].DlikeNum";
		let state = !this.data.comment[needindex].isLike,
			num = this.data.comment[needindex].DlikeNum;
		if (state) {
			num++;
			wx.request({
				url: baseUrl + '/api/dynamic/setlike',
				data: {
					DCid: Did,
					Uid,
					type
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		} else {
			num--;
			wx.request({
				url: baseUrl + '/api/dynamic/cancellike',
				data: {
					DCid: Did,
					Uid,
					type
				},
				method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
				// header: {}, // 设置请求的 header
				success: function (res) {
					// console.log(res);
					// success
				},
				fail: function () {
					// fail
				},
				complete: function () {
					// complete
				}
			});
		}
		this.setData({
			[changeLike]: state,
			[changeNum]: num
		});
	},
	longtap(e) {
		// console.log(e);
		// console.log(this);
	},
	// showModal(e) {
	// 	this.setData({
	// 		showModal:true
	// 	})
	//   },
	hideModal(e) {
		this.setData({
			// modalName: null
			showModal: false
		})
	},
	hideandcancel() {
		this.setData({
			// modalName: null
			showModal: false
		})
		let ifAttention = this.data.theme.attention;
		const Uid = app.globalData.loginInfo.Uid;
		let that = this;
		wx.request({
			url: baseUrl + '/api/user/cancelattention',
			data: {
				Uid: Uid,
				attention: 1
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				let themeattention = "theme.attention";
				if (res.data.code === 200) {
					that.setData({
						[themeattention]: false
					});
				}
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		});
	},
	// 遮罩层延迟
	//  下拉刷新新加一个view， 平时隐藏，刷新的时候，增加标志位，渲染，其余时候不显示
	// 长按事件 longtap  但是长按事件bindlongtap与点击事件bindtap的冲突
	jump: function (e) {
		let Did = e.currentTarget.dataset.did;
		let type = e.currentTarget.dataset.type;
		wx.navigateTo({
			//  url: 'test?id=1' 可以带参数 dataset里面的只有小写
			url: '../laf_comment/laf_comment?did=' + Did + '&type=' + type,
			success: function (res) {
				// success
			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		})
	},
	callback() {
		common.goBack();
	}
});